import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
public class Client
{
	private static Socket socket;
	private static PrintWriter printWriter;
	private static String inputLine;
	public static int flagaClient, flagaServer;

	Client(int flagaClient) {
		try
		{
			//socket = new Socket("localhost",63401);
			Socket socket = new Socket();
			// This limits the time allowed to establish a connection in the case
			// that the connection is refused or server doesn't exist.
			socket.connect(new InetSocketAddress("localhost",63401), 1000);
			// This stops the request from dragging on after connection succeeds.
			//socket.setSoTimeout(1000);
			PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			DataOutputStream output = new DataOutputStream(socket.getOutputStream());
			DataInputStream input = new DataInputStream(socket.getInputStream());
			
			output.writeInt(flagaClient);
			
			flagaServer = input.readInt();
			
			if (flagaServer == 1) {
				printWriter.println(Screen.nick);
				printWriter.println(Data.czasAll);
				printWriter.println(Data.dataNF);		
				
			}
			
			if (flagaServer == 2) {
				Statystyki.statyst = "";
				while((inputLine = bufferedReader.readLine()) != null)
					Statystyki.statyst += inputLine + "\n";		
				//socket.close();
			}
			socket.close();
		}
		catch(Exception e)
		{
			System.out.println(e);
			Statystyki.timeout = true;
		}
	}
}

